package lib.bone.flow.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.utils.JsonUtil;
import lib.bone.flow.dao.FlowWorkDAO;
import lib.bone.flow.domain.dto.FlowParam;
import lib.bone.flow.domain.entity.FlowWork;
import lib.bone.flow.service.FlowWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class FlowWorkServiceImpl implements FlowWorkService {

    @Autowired
    FlowWorkDAO flowWorkDAO;


    @Override
    public List<FlowWork> getFlowWorksByIds(List<String> flowWorkIds) {
        return flowWorkDAO.findByIdIn(flowWorkIds);
    }

    @Override
    public void deleteFlow(String flowWorkId) {
        // 触发删除事件
//        if (!Validator.isEmpty(removeEvent)){
//            removeEvent.process(flowWorkId);
//        }
        flowWorkDAO.deleteById(flowWorkId);
    }



    @Override
    public FlowWork saveOne(FlowWork flowWork) {
        return flowWorkDAO.save(flowWork);
    }

    @Override
    public Optional<FlowWork> getById(String flowWorkId) {
        return flowWorkDAO.findById(flowWorkId);
    }

    @Override
    public ResponseDTO<FlowWork> updateWork(FlowWork flowWork) {
        if (Validator.isEmpty(flowWork.getId())) {
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM, "ID不可为空");
        }
        return ResponseDTO.succData(flowWorkDAO.save(flowWork));
    }

    @Override
    public ResponseDTO getAll(Pageable pageable, Example<FlowWork> example) {
        Page<FlowWork> page;
        //是否为空 --
        if (Validator.isEmpty(example)) {
            page = flowWorkDAO.findAll(pageable);
        } else {
            page = flowWorkDAO.findAll(example, pageable);
        }
        List<FlowWork> list = page.getContent();
        PageResultDTO<FlowWork> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public List<FlowWork> getPaperNumber(String flowDefineCode, String initiator) {
        List<FlowWork> flowWorkList = flowWorkDAO.findByFlowDefineCodeAndInitiator(flowDefineCode, initiator);
        return flowWorkList;

    }

    @Override
    public List<FlowWork> getFlowWorks(String flowDefineCode, String currentNodeId,String flowWorkStatus){
        return flowWorkDAO.findByFlowDefineCodeAndCurrentNodeIdAndFlowWorkStatus(flowDefineCode, currentNodeId,flowWorkStatus);
    }


    @Override
    public List<FlowWork> getAllFlowWork(Example<FlowWork> example) {
        return flowWorkDAO.findAll(example);
    }

    @Override
    public List<FlowWork> getAllFlowWork() {
        return flowWorkDAO.findAll();
    }

    @Override
    public Optional<FlowWork> getByServiceId(String id) {
        return flowWorkDAO.findByServiceId(id);
    }


    @Async("threadExecutor")
    @Override
    public void buildService(FlowWork flowWork) {
        String flowParams = flowWork.getFlowParam();
        FlowParam param = JsonUtil.string2Obj(flowParams,FlowParam.class);
        assert param != null;
        String serviceName = param.getServiceName();
        String serviceType = param.getServiceType();
        String serviceId = param.getServiceId();
        flowWork.setServiceName(serviceName);
        flowWork.setServiceType(serviceType);
        flowWork.setServiceId(serviceId);
        log.info("更新serivceName:{},更新serivceId:{},更新serivceId:{}",serviceName,serviceId,serviceType);
        flowWorkDAO.save(flowWork);
    }

    @Async("threadExecutor")
    @Override
    public void buildCycle(FlowWork flowWork, String cycleId, String cycleName) {
        flowWork.setCycleId(cycleId);
        flowWork.setCycleName(cycleName);
        log.info("{}更新周期为{}",flowWork.getServiceName(),cycleName);
        flowWorkDAO.save(flowWork);
    }

    @Override
    public List<FlowWork> findAllByCurrentNodeId(String currentNodeId) {
        return flowWorkDAO.findAllByCurrentNodeId(currentNodeId);
    }

    @Override
    public List<FlowWork> findKycCheck(String flowName, String flowWorkStatus) {
        return flowWorkDAO.findKycCheck(flowName,flowWorkStatus);
    }
}
